package com.appengine.news.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appengine.news.entity.RssFeedEntity;
import com.appengine.news.utils.StringUtils;
import com.google.appengine.api.datastore.Text;
/**
 * Manage service
 * @author Aliaksandr_Spichakou
 *
 */
public class ManageService extends AbstractService{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5782516397897254371L;
	private static final Logger log = Logger.getLogger(ManageService.class
			.getName());
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		log.entering(ManageService.class.getName(), "service");
		final String command = req.getParameter(COMMAND);
		
		if(ADD_COMMAND.equalsIgnoreCase(command))
		{
			final String url = req.getParameter(VALUE_URL);
			if(url!=null && url.length()!=0)
			{
				final String freqStr = req.getParameter(VALUE_FREQ);
				final Long freq = StringUtils.parseString2Long(freqStr);
				addRSS(url,freq);
			}
		}
		if(REMOVE_COMMAND.equalsIgnoreCase(command))
		{
			final String strId = req.getParameter(VALUE_ID);
			final Long id = StringUtils.parseString2Long(strId);
			if(id==null)
			{
				log.log(Level.SEVERE ,"Given id is wrong: ");	
				return;
			}
			removeRss(id);
		}	
		res.sendRedirect(MANAGE_TASK);
		log.exiting(ManageService.class.getName(), "service");
	}
	/**
	 * Remove RSS feed
	 * @param id
	 */
	private void removeRss(Long id)
	{
		log.entering(ManageService.class.getName(), "removeRss");
		final PersistenceManager persistenceManager = getPersistenceManager();
		final String query = "select from " + RssFeedEntity.class.getName() + " where id==" + id;
		final List<RssFeedEntity> list = (List<RssFeedEntity>)persistenceManager.newQuery(query).execute();
		if(!list.isEmpty())
		{
			final RssFeedEntity feed = list.get(0);
			persistenceManager.deletePersistent(feed);
		}
		persistenceManager.close();
		log.exiting(ManageService.class.getName(), "removeRss");
	}
	/**
	 * Add RSS feed
	 * @param url
	 */
	private void addRSS(String url,Long freq)
	{
		log.entering(ManageService.class.getName(), "addRSS");
		try {
			new URL(url);
		} catch (MalformedURLException e) {
			log.log(Level.SEVERE ,"Given url is wrong: ", e);			
			return;
		}
		final PersistenceManager persistenceManager = getPersistenceManager();
		final RssFeedEntity feed = new RssFeedEntity();
		feed.setUrl(url);
		feed.setTitle(new Text(url));
		feed.setSubtitle(new Text(url));
		feed.setLastUpdate(new Date(0));
		if(freq==null)
		{
			feed.setUpdateFreq(new Long(F_UPDATE_HOUR));
		}
		else
		{
			feed.setUpdateFreq(freq);
		}
		persistenceManager.makePersistent(feed);
		persistenceManager.close();		
		log.exiting(ManageService.class.getName(), "addRSS");
	}	
}
